/**根据给定的毫秒数算出这个区间的天、小时、分钟、秒的具体描述
  * @class Duration
 */

export default class Duration {
  private durationNum: number
  /**
   * 一分钟的毫秒数
    * @memberof Duration
    * @static {number} minuteMIllisecond */
  static minuteMillisecond = 1000 * 60

  /**一小时的毫秒数
    * @memberof Duration
    * @static {number} hourMillisecond */
  static hourMillisecond = Duration.minuteMillisecond * 60

  /**一天的毫秒数
    * @memberof Duration
    * @static {number} dayMillisecond */
  static dayMillisecond = Duration.hourMillisecond * 24

  /**
    * 创建一个duration
    * @constructor
    * @param {number}} durationNum
    */
  constructor (durationNum: number, public isCN = false) {
    this.durationNum = durationNum
  }

  /**设置时间量（毫秒） */
  setDuration (duration: number) {
    this.durationNum = duration
  }

  /**直接获取中文字符串*/
  static getStringCn (durationNum: number) {
    return (this.constructor(durationNum, true)).toString()
  }

  /**{number} days 天的getter	 */
  get days () {
    return (this.durationNum / Duration.dayMillisecond).toFixed(2)
  }

  /**{number} hours 小时的getter	 */
  get hours () {
    return (this.durationNum / Duration.hourMillisecond).toFixed(2)
  }

  /**{number} minuts 分钟的getter	 */
  get minutes () {
    return (this.durationNum / Duration.minuteMillisecond).toFixed(2)
  }

  /**@returns {number} minuts 分钟的getter	 */
  get seconds () {
    return (this.durationNum / 1000).toFixed(2)
  }

  /** 默认返回值的描述
    * @return {string}
    */
  toString () {
    const days = parseInt(String(this.durationNum / Duration.dayMillisecond))
    const dayRemainse = this.durationNum % Duration.dayMillisecond

    const hours = parseInt(String(dayRemainse / Duration.hourMillisecond))
    const hourRemainse = dayRemainse % Duration.hourMillisecond

    const minutes = parseInt(String(hourRemainse / Duration.minuteMillisecond))

    const minuteRemainse = hourRemainse / Duration.minuteMillisecond
    if (this.isCN) {
      return `${days} 天 ${hours} 小时 ${minutes} 分钟 ${(minuteRemainse / 1000).toFixed(2)} 秒`
    }
    return `${days} days ${hours} hours ${minutes} minutes ${(minuteRemainse / 1000).toFixed(2)} seconds`
  }
}
